package Capitulo09.Exercicios;

class Waiting implements Runnable {
	boolean flag = false;

	public synchronized void run() {

		if (flag) {
			flag = false;
			System.out.println( Thread.currentThread().getName() + " : 1 - " + flag );
			try {
				this.wait();
			} catch (Exception e) {}
			System.out.println(Thread.currentThread().getName() + " : 2 - " + flag);
		} else {
			flag = true;
			System.out.println(Thread.currentThread().getName() + " : 3 - " + flag);
			try {
				Thread.sleep(1000);
			} catch (Exception e) {}

			System.out.println(Thread.currentThread().getName() + " : 4 - " + flag);
			notify();
			//notifyAll();
		}
	}

	public static void main(String[] args) {

		Waiting w = new Waiting();

		Waiting w1 = new Waiting();

		Thread t1 = new Thread(w);
		t1.setName("Thread - 1");
		t1.start();

		Thread t2 = new Thread(w);
		t2.setName("Thread - 2");
		t2.start();

		Thread t3 = new Thread(w1);
		t3.setName("Thread - 3");
		//t3.start();

		Thread t4 = new Thread(w1);
		t4.setName("Thread - 4");
		//t4.start();
	}
}